// 封装滚动懒加载
import BScroll from 'better-scroll'
import http from 'Utils/http'
import { Toast } from 'mint-ui'
import _ from 'lodash'

const scroll = ({
    el,
    arr,
    url,
    method,
    params,
    dataList,
    dataType
}) => {
    let count = 0
    let scroll = new BScroll(el, {
        probeType: 1,
        pullUpLoad: {
            threshold: 50
        }
    })

    // 默认是关闭的，需要先开启上拉加载
    scroll.on('pullingUp', () => {
        if (count < arr.length) {
            // 数据请求
            http({
                    url,
                    method,
                    params: {
                        token: params.token,
                        movieIds: arr[count].join(',')
                    }
                })
                .then(res => {
                    console.log(res);
                    dataList.push(...res.data[dataType])
                })
        }

        scroll.finishPullUp()

        if (count == arr.length) {
            // 到底了
            Toast({
                message: '到底咯',
                position: 'bottom',
                duration: 1000
            });
            return
        }
        count++
    })
}

export default scroll